package com.int_tech.control.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Created by liubochao on 2018/7/27.
 */
public class TestLocationToBaidu {

    public static void main(String[] arg){
        String x = "116.95252061596868";
        String y = "40.075139137255434";

       /* String url = "http://api.map.baidu.com/geoconv/v1/?coords="+x+","+y+"&from=1&to=5&ak=TsCP2qXsP0RLmchQ7cQYyQa1C5Cwh4er";

        String result = getJson(url);

        JSONObject object = JSONObject.fromObject(result);

        JSONArray result1 = (JSONArray) object.get("result");

        object = result1.getJSONObject(0);

        x = String.valueOf((Double) object.get("x"));
        y = String.valueOf((Double) object.get("y"));


        System.out.print(x);
        System.out.print(y);*/

       System.out.print(map_tx2bd(Double.parseDouble(x),Double.parseDouble(y)));


    }


    public static String map_tx2bd(double lat, double lon){
        double bd_lat;
        double bd_lon;
        double x_pi=3.14159265358979324;
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00052 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000006 * Math.cos(x * x_pi);
        bd_lon = z * Math.cos(theta) + 0.0065;
        bd_lat = z * Math.sin(theta) + 0.006;

        System.out.println("bd_lat:"+bd_lat);
        System.out.println("bd_lon:"+bd_lon);
        return bd_lat+","+bd_lon;
    }


    public static String getJson(String urlstr) {
        org.apache.http.client.HttpClient httpclient = new DefaultHttpClient();
        HttpGet method = new HttpGet(urlstr);
        method.setHeader("Content-type" , "application/json");
        HttpResponse response = null;
        try {
            response = httpclient.execute(method);
            if (response != null) {
                // HttpEntity resEntity = response.getEntity();
                return EntityUtils.toString(response.getEntity());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
